import java.util.*;

/**
 * Represents a complete adventure.
 */
public class Adventure {
    private Vector nodes = new Vector();
    private GameNode currentNode = null;
    private String title = "Unknown Adventure";
    private String author = "Anonymous";
    private String place = null;
    private String creationDate = null;
    private String description = null;
    private int score = 0;
	
    Adventure() {
    }
	
    Adventure(String title) {
		setTitle(title);
    }
	
    public void setAuthor(String author) {
		this.author = author;
    }

    public String getAuthor() {
		return this.author;
    }
	
    public void setPlace(String place) {
		this.place = place;
    }
	
    public String getPlace() {
		return this.place;
    }
	
    public void setDescription(String desc) {
		this.description = desc;
    }
	
    public String getDescription() {
		return this.description;
    }

    public int getScore() {
		return this.score;
    }
	
    public void increaseScore(int addScore) {
		score += addScore;
    }

    /** Where is the player at this moment? */
    public GameNode getCurrentNode() {
		return currentNode;
    }
    
    /** Sets the actual node via its index. */
    private void setCurrentNode(int currentNode) {
		this.currentNode = getNode(currentNode);
    }

    /** Sets the actual node directly.  */
    private void setCurrentNode(GameNode currentNode) {
		this.currentNode = currentNode;
    }
    
    /** Adds a node to the game. */
    public void addNode(GameNode newNode) {
		nodes.addElement(newNode);
		if (newNode.isStartNode()) {
			setCurrentNode(newNode);
		}
    }
    
    /** Give a node via its index. */
    public GameNode getNode(int nodeNumber) {
		return (GameNode) nodes.elementAt(nodeNumber);
    }

	/**
	 * Returns the last finished GameNode in this adventure.
	 */
	public GameNode getLastNode() {
		return this.currentNode.getLastNode();
	}
    
    /** Sets the adventures title. */
    public void setTitle(String title) {
		this.title = title;
    }
    
    /** Gives the adventures title back. */
    public String toString() {
        return this.title;      
    }

    /** 
     * Chooses a selection from the old current node and makes it the new current
     * node, if it is a right selection with a target node.
     * A right selection increases the users game score. 
	 * selectin == -1 means a general wrong selection.
	 * 
     */
    public void chooseSelection(int selection) {
		System.out.println("Player selected solution <" + selection + ">.");
		GameNode nextGameNode = getCurrentNode().chooseSolution(selection);
		if (nextGameNode != null) {
			if (!currentNode.isFinished()) {
				System.out.println("Hey, you got score in <" + currentNode + ">: " + currentNode.getScore());
				this.increaseScore(currentNode.getScore());
				System.out.println("Overall score until now: " + this.getScore());
			} else {
				System.out.println("Sorry, you got no score in <" + currentNode + ">, because you did already finish this node before.");
			}
			this.currentNode.setFinished();
			nextGameNode.setLastNode(this.currentNode);
			this.currentNode = nextGameNode;
		} else {
			System.out.println("Ooops, wrong answer...");
		}
    }
}